<?php

$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_bind($server, '127.0.0.1', '8889');
socket_listen($server);

while (true) {
    $client = socket_accept($server);
    if (!$client) {
        continue;
    }

    $request = socket_read($client, 1024);
    $http = new HttpProtocol();
    try {
        $http->request($request);
        $http->response('success');
        socket_write($client, $http->responseData);
        socket_close($client);
    } catch (\Exception $exc) {
        echo $exc->getMessage();
    }
    echo socket_strerror(socket_last_error($server)) . "\n";
}